/*
 * Game.h
 *
 *  Created on: Nov 15, 2012
 *      Author: lucas
 */

#ifndef GAME_H_
#define GAME_H_


#include <list>
#include <deque>
#include <map>
#include "GameSettings.h"
#include "ThreadSafetyQueue.h"
#include "ConditionVariable.h"
#include "PhysicsEngine.h"
#include "BirdGenerator.h"
#include "EggGenerator.h"
#include "Thread.h"
#include "Mutex.h"
#include "Lock.h"
class Level;
class Vector2D;
class Egg;
class VisibleElement;
class Pig;
class Match;
class Surface;
class Player;

class Game : public Thread {
private:
	std::list<VisibleElement*> entities;

	ThreadSafetyQueue<Pig*> freePigsQueue;
	Surface* surface;
	Level* level;
	std::map<Player*, Pig*> players;
	PhysicsEngine pe;
	BirdGenerator bg;
	EggGenerator eg;
	int playingtime;	//en ticks
	int finishtime;		//en ticks
	Mutex gameMutex;
	Mutex matchMutex;
	Match* match;
	ConditionVariable gameCV;
	bool ended;
	bool won;

	void deleteDisposableObjects();
	bool isOutOfRange(VisibleElement* e);
	void loadLevel(Player* p);
	void generateMatch();
	bool checkPigsHealth();
	bool checkHeapHealth();
public:
	ThreadSafetyQueue<Egg*> eggsQueue;
	Game(Level* l, Player* p);
	virtual ~Game();
	void play();
	void ejecutar();
	bool addPlayer(Player* player);
	void removePlayer(Player* player);
	void addEgg(eggTypes egg, double x, double y, Vector2D speed);
	void addVisibleElement(VisibleElement* e);
	eggTypes getNextEgg();
	Match* getMatch();
	void addPig(Pig* p);
	int getPlayers();
	void pause();
	bool gameEnded();
	bool gameWon();
};

#endif /* GAME_H_ */
